package com.manager.system.controller;

import com.manager.common.annotation.AspectLogInfo;
import com.manager.common.constant.AspectMethodOperationEnum;
import com.manager.common.constant.OperationLogEnumType;
import com.manager.common.token.TokenUtils;
import com.manager.common.utils.ObjectUtils;
import com.manager.common.vo.PageVO;
import com.manager.common.vo.ResultVO;
import com.manager.model.System_User;
import com.manager.system.service.PermissionService;
import com.manager.system.service.UserService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {

   @Autowired
   private UserService userService;

    @Autowired
    private PermissionService permissionService;


    @PostMapping("/getUserPage")
    @ResponseBody
    public PageVO getUserPage(System_User user, HttpServletRequest request, HttpServletResponse response) throws IOException {
        PageVO pageVO = userService.getUserMap(user);
        return  pageVO;
    }

    @PostMapping("/addOrUpdate")
    @ResponseBody
    @AspectLogInfo(tableName="tb_sys_user",content="添加或修改用户数据",type= AspectMethodOperationEnum.SAVE, logType = OperationLogEnumType.USER)
    public ResultVO addOrUpdate(System_User model, HttpServletRequest request, HttpServletResponse response) throws IOException{
        JSONObject json = new JSONObject();
        try{

            if(StringUtils.isNotEmpty(model.getId())){
                userService.updateUser(model);
            }else{
                userService.addUser(model);
            }
            return ResultVO.success();
        }catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
            return ResultVO.failed(e.getMessage());
        }
    }
    @PostMapping("/deleteUser")
    @ResponseBody
    @AspectLogInfo(type= AspectMethodOperationEnum.DELETE,tableName="tb_sys_user", content="删除用户信息", logType = OperationLogEnumType.USER)
    public ResultVO deleteUser(System_User model, HttpServletRequest request, HttpServletResponse response) throws IOException{
        JSONObject json = new JSONObject();
        try{
            userService.deleteUser(model);
            return ResultVO.success();
        }catch (Exception e) {
            e.printStackTrace();
        }
        return ResultVO.failed();
    }
    @PostMapping("/getUserTree")
    @ResponseBody
    public ResultVO getUserTree(HttpServletRequest request, HttpServletResponse response) throws IOException{
        JSONArray arr = userService.getUserJSONArray();
        return ResultVO.success(arr);
    }

    @RequestMapping("/getUserMenuData")
    public ResultVO getUserMenuData(HttpServletRequest request, HttpServletResponse response) throws IOException{
        System_User user = TokenUtils.getLoginUser();
        System.out.println(user.getUserName());
        //未登录
        if(user == null){
            return ResultVO.failed();
        }
        JSONObject jsonObject;
    //超级管理员
        if(user.checkAdmin()){
            jsonObject = permissionService.getAdminMenuJsonArr();
        }else{//普通用户
            jsonObject =permissionService.getUserMenuJsonArr(user);
        }
        return ResultVO.success(jsonObject);
    }

    @RequestMapping("/getMenuTree")
    @ResponseBody
    public ResultVO getMenuTree(HttpServletResponse response, System_User model) throws IOException{
        JSONArray jsonArray = userService.getMenuTree(model);
        return ResultVO.success(jsonArray);
    }

    @RequestMapping("/getSystemPermissionList")
    public ResultVO getSystemPermissionList(String name){
        return ResultVO.success(userService.getSystemPermissionList(name));
    }

    /*@RequestMapping("/updatePassword")
    public void updatePassword(System_User model , HttpServletResponse response) throws IOException{
        JSONObject jsonObject = new JSONObject();
        model.setUserName(RequestHolder.getCurrentUser().getUserName());
        model.setPassword(MD5.getMD5(model.getPassword().getBytes()));
        System_User system_User =  loginService.getSystemUser(model);
        if(system_User == null){
            jsonObject.put("status", 2);
        }else{
            try{
                model.setPassword(MD5.getMD5(model.getString1().getBytes()));
                userService.updateUserPwsd(model);
                jsonObject = getResultJSON(1);
            }catch (Exception e) {
                // TODO: handle exception
                e.printStackTrace();
                jsonObject = getResultJSON(0);
            }
        }
        outData(response, jsonObject);
    }*/

    @RequestMapping("/getUserArray")
    public ResultVO getUserArray(HttpServletResponse response) throws IOException{
        JSONArray jsonArray = userService.getUserArray();
        return ResultVO.success(jsonArray);
    }

    @RequestMapping("/getSystemUserList")
    @ResponseBody
    public ResultVO getSystemUserList(System_User model, HttpServletResponse response, HttpServletRequest request) throws Exception{
        List<System_User> list = userService.getSystemUserList();
        return ResultVO.success(ObjectUtils.getJSONArray(System_User.config,list));
    }
}
